<template>
	<view class="container">
		<view class="search-top-wrapper">
			<view class="search-wrapper">
				<input class="uni-input" v-model="shopName" confirm-type="搜索" placeholder="请输入成员名字"
					@confirm="handleSearch" />
				<view @click="handleSearch" class="iconBox">
					<image src="/static/search.png" class="icon" @click="handleSearch"></image>
				</view>
			</view>
			<view class="tabs">
				<view class="tab-item">
					<picker @change="regionChange2" :value="index" :range="industryTypeList" range-key="label">
						{{leibie}}<image src="/static/arrow-down@2x.png" class="iamge"></image>
					</picker>
				</view>
				<view class="tab-item" @click="togglePopup" >
						{{region}}<image src="/static/arrow-down@2x.png" class="iamge"></image>
				</view>
			</view>
		</view>
		
		<view class="list">
			<view class="item" v-for="(item,index) in storeList" :key="index" @click="toStoreInfo(item)">
				<image :src="item.image" class="iamge"></image>
				<view class="item-info">
					<view class="name">{{item.name}}</view>
					<view class="address" @click.stop="handleOpenLocation(item)"><image src="/static/location@2x.png" class="iamge"></image>{{item.address}}</view>
					<view class="address phone" @click.stop="handlePhoneCall(item.phone)"><image src="/static/call-calling@2x.png" class="iamge"></image>{{item.phone}}</view>
					<view class="count">联盟折扣：<text>{{item.innerPhone}}</text>折</view>
				</view>
			</view>
		</view>
		<region-picker v-if="(selectAreaId && id) || !id" :isPopupShow.sync="isPopupShow"
		 :selectProvinceId.sync="selectProvinceId"
		 :initFlag.sync="initFlag"
		 :selectCityId.sync="selectCityId"
		 :selectAreaId.sync="selectAreaId"
		:region.sync="region">
		</region-picker>
	</view>
</template>

<script>
import {
		selectShopDistance,
		selectAllIndustryType
} from '@/api/store.js'
import { getAllProvinces, getByParentIds} from '@/api/address.js'
import regionPicker from '@/components/region-picker/index.vue';
		
	export default {
		components: {
			regionPicker
		},
		data() {
			return {
				maskClass: {
					'position': 'fixed',
					'bottom': 0,
					'top': 0,
					'left': 0,
					'right': 0,
					'z-index': 10,
					'background-color': 'rgba(0, 0, 0, 0.6)'
				},
				popupTransfromClass: {
					'position': 'fixed',
					'bottom': 0,
					'left': 0,
					'right': 0,
					'z-index': 11,
					'background-color': '#fff'
				},
				shopName:'',
				form: {
					current: 1,
					default: true,
				},
				storeList:[],
				industryTypeList:[],
				areaId: '',
				cityId: '',
				provinceId: '',
				value: [0, 0, 0], // 地址选择器省市区 暂存 currentIndex
				regionValue: [0, 0, 0], // 地址选择器省市区 最终 currentIndex
				provinces: [], // 一级地址
				citys: [], // 二级地址
				areas: [], // 三级地址
				isCanConfirm: true ,//是否禁止在第一列滚动期间点击确定提交数据
				isPopupShow: false,
				region: '区域',
				selectProvinceId: '',
				selectCityId: '',
				selectAreaId: '',
				leibie:'类别'
			};
		},
		onLoad(option) {
			this.getList();
			this.selectAllIndustryType1();
			// this.getProvince();
			this.selectProvinceId = option.selectProvinceId;
			this.selectCityId = option.selectCityId;
			this.selectAreaId = option.selectAreaId;
			this.address = option.address;
			if (option.loginType) {
				this.loginType = option.loginType
			}
			// this.setTitle(option);
		},
		onShow() {
			if (this.id) {
				this.initFlag = true;
			} else {
				this.initFlag = false;
			}
		},
		watch:{
			selectAreaId(val){
				this.handleSearch()
			}
		},
		methods: {
			toStoreInfo(item){
				const url = '/pages/store/storeinfo'
				uni.navigateTo({
					url: `${url}?imgPath=${encodeURIComponent(item.infoImage)}&shopId=${item.id}`
				})
			},
			handlePhoneCall(phoneNumber) {
				uni.makePhoneCall({
					phoneNumber
				});
			},
			convert2TecentMap(lng, lat) {
				if (lng == '' && lat == '') {
					return {
						lng: '',
						lat: ''
					}
				}
				var x_pi = 3.14159265358979324 * 3000.0 / 180.0
				var x = lng - 0.0065
				var y = lat - 0.006
				var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
				var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
				var qqlng = z * Math.cos(theta)
				var qqlat = z * Math.sin(theta)
				return {
					lng: qqlng,
					lat: qqlat
				}
			},
			handleOpenLocation(item) {
				const that = this;
				const {
					latitude,
					longitude,
					name,
					cityName,
					districtName,
					address
				} = item;
				let lng = that.convert2TecentMap(longitude, latitude).lng
				let lat = that.convert2TecentMap(longitude, latitude).lat
				uni.openLocation({
					latitude: lat,
					longitude: lng,
					name,
					address: address
				});
			},
			togglePopup() {
				if (this.initFlag) return;
				this.isPopupShow = !this.isPopupShow;
				// this.getList(0,this.shopName,this.selectAreaId)
			},
			regionChange2(e) {
				console.log(e)
				this.getList(this.industryTypeList[e.detail.value].label,this.shopName,this.selectAreaId)
				this.leibie=this.industryTypeList[e.detail.value].label
			},
			handleSearch() {
				console.log(this.shopName)
				this.getList(0,this.shopName,this.selectAreaId) 
			},
			selectAllIndustryType1() {
				selectAllIndustryType().then(respanse => {
					let temp = [];
					let obj1 = {};
					obj1.label='默认';
					obj1.value='0';
					temp.push(obj1);
					respanse.result.forEach(item=>{
						let obj = {};
						obj.label= item.name;
						obj.value= item.id+'';
						temp.push(obj);
					})
					this.industryTypeList = temp
				})
			},
			getList(value,shopName,areaId) {
				const that = this
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						const {
							latitude,
							longitude
						} = res
						const param = {
							latitude,
							longitude,
							pageNum: 1,
							pageSize: 10,
							type: 2,
							industryType: value,
							shopName:shopName,
							districtId:areaId
						}
						// 金手联盟
						selectShopDistance(param).then(respanse => {
							that.storeList = respanse.result.rows
						})
					},
					fail: error => {
						console.log("获取定位失败了", error)
						uni.showModal({
							title: '提示',
							content: '获取定位失败，是否授权打开定位',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting({
										success(res) {
											console.log(res.authSetting)
										}
									})
								}else{
									const res={
										latitude:120.13,
										longitude:30.12
									} 
									const param = {
										latitude:120.13,
										longitude:30.12,
										pageNum: 1,
										pageSize: 10,
										type:2,
										industryType: value,
										shopName:shopName,
										districtId:areaId
									}
									// 附近商家
									selectShopDistance(param).then(respanse => {
										that.storeList = respanse.result.rows
									})
									
									
								}
							}
						})
					}
				});
			},
			submit() {
				// if(!this.form.)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-top:196rpx;
		padding-bottom:32rpx;
		.search-top-wrapper {
			position: fixed;
			top: var(--window-top);
			left: 0;
			right: 0;
			z-index: 2;
			padding: 20rpx 48rpx 0;
			background-color: #F5F5F5;
		
			.search-wrapper {
				position: relative;
				height: 88rpx;
				border-radius: 32rpx;
		
				.iconBox {
					position: absolute;
					left: 10rpx;
					width: 100rpx;
					top: 50%;
					transform: translateY(-50%);
					height: 50rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					z-index: 111;
		
					.icon {
		
						width: 48rpx;
						height: 40rpx;
					}
				}
		
		
				.uni-input {
					display: block;
					padding-right: 20rpx;
					padding-left: 100rpx;
					width: 100%;
					height: 88rpx;
					box-sizing: border-box;
					font-size: 26rpx;
					color: #333;
				}
		
				background-color: #fff;
			}
			.tabs{
				display: flex;
				justify-content: space-around;
				align-items: center;
				height: 88rpx;
				.tab-item{
					display: flex;
					align-items: center;
					font-size: 28rpx;
					font-weight: 500;
					color: #666666;
					line-height: 36rpx;
					image{
						width: 20rpx;
						height: 20rpx;
					}
				}
			}
		}
		.list{
			padding: 0 32rpx;
			.item{
				padding: 14rpx 26rpx 8rpx 32rpx;
				margin-bottom: 20rpx;
				display: flex;
				background-color: #fff;
				border-radius: 16rpx;
				.iamge{
					width: 128rpx;
					height: 128rpx;
					border-radius: 16rpx;
					min-width: 0;
				}
				.item-info{
					flex: 1;
					position: relative;
					margin-left: 24rpx;
					.name{
						font-size: 28rpx;
						font-weight: 600;
						color: #151B33;
						line-height: 36rpx;
					}
					.address{
						margin-top: 12rpx;
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						line-height: 24rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						-webkit-line-clamp: 2;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						&.phone{
							margin-top: 16rpx;
						}
						image{
							width: 24rpx;
							height: 24rpx;
						}
					}
					.count{
						position: absolute;
						right: 0;
						bottom: 4rpx;
						font-size: 24rpx;
						font-family: DM Sans-Bold, DM Sans;
						font-weight: bold;
						color: #FF4747;
						line-height: 38rpx;
						font-style: italic;
						text{
							font-size: 38rpx;
							padding-right: 5rpx;
						}
					}
				}
			}
		}
	}
</style>